package com.hcd.weixin.controller;

import com.hcd.weixin.common.context.EmpHolder;
import com.hcd.weixin.domain.Result;
import com.hcd.weixin.domain.Task;
import com.hcd.weixin.domain.dto.CancelTaskDto;
import com.hcd.weixin.domain.vo.Pager;
import com.hcd.weixin.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

// 工单
@RestController
@RequestMapping("/task-service/task")
@Slf4j
public class TaskController{

    @Autowired
    private TaskService taskService;


    /**
     * 搜索工单
     *
     * @param pageIndex
     * @param pageSize
     * @param pileSn 设备编号
     * @param empId    工单所属人Id
     * @param taskCode  工单编号
     * @param taskStatus    工单状态
     * @return
     */
    @GetMapping("/search")
    public Result search(
            @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Long pageIndex,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Long pageSize,
            @RequestParam(value = "pileSn", required = false, defaultValue = "") String pileSn,
            @RequestParam(value = "empId", required = false, defaultValue = "") Integer empId,
            @RequestParam(value = "taskCode", required = false, defaultValue = "") String taskCode,
            @RequestParam(value = "taskStatus", required = false, defaultValue = "") Integer taskStatus,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        Pager<Task> searched = taskService.search(pageIndex, pageSize, pileSn, empId, taskCode, taskStatus, start, end);
        return Result.success(searched);
    }

    // 获取用户排名情况
    @GetMapping("/rank/{empId}")
    public Result rank(@PathVariable("empId") Long empId) {
        Map<String, Object> result = new HashMap<>();
        result.put("total", 3);
        result.put("rank", 3);
        return Result.success(result);
    }


    // 接受工单
    @GetMapping("/accept/{taskId}")
    public Result accept(@PathVariable("taskId") Long taskId ){
        // 获取登录人id
        Integer empId = EmpHolder.get();
        // 调用service
        Boolean accept = taskService.accept(taskId, empId);
        if (!accept) {
            return Result.error("接受工单失败");
        }
        return Result.success("接受工单成功",null);
    }


    // 拒绝/取消工单
    @PostMapping("/cancel/{taskId}")
    public Result cancel(@PathVariable("taskId")Long taskId,@RequestBody CancelTaskDto cancelTaskDto){
        // 获取登录人id
        Integer userId = EmpHolder.get();
        // 调用service
        Boolean cancel = taskService.cancel(taskId, cancelTaskDto, userId);
        if (!cancel) {
            return Result.error("取消/拒绝工单失败");
         }
        return Result.success("取消/拒绝工单成功",null);
    }


    // 完成工单
    @GetMapping("/complete/{taskId}")
    public Result complete(@PathVariable("taskId")Long taskId){
        // 获取登录人id
        Integer empId = EmpHolder.get();
        // 调用service
        Boolean complete = taskService.complete(taskId, empId);
        if (!complete){
            return Result.error("完成工单失败");
        }

        return Result.success("完成工单成功",null);
    }

    /**
     * 获取工单详情
     * @param taskId
     * @return
     */
    @GetMapping("/{taskId}")
    public Result get(@PathVariable("taskId")Long taskId){
        return Result.success(taskService.getById(taskId));
    }
}
